package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Authority;
import cn.sunlelin.educationserver.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @PostMapping("/authority/createAuthority")
    public Map<String,Object> createAuthority(@RequestBody Authority authority){
        Map<String,Object> map=new HashMap<>();
        if(authorityService.createAuthority(authority)){
            map.put("code",200);
            map.put("msg","创建用户权限成功");
            map.put("object",null);
        }
        else {
            map.put("code",500);
            map.put("msg","创建用户权限失败，该用户已有权限信息");
            map.put("object",null);
        }
        return map;
    }

    @PostMapping("/authority/updateAuthority")
    public Map<String,Object> updateAuthority(@RequestBody Authority authority){
        Map<String,Object> map=new HashMap<>();
        if(authorityService.updateAuthority(authority)){
            map.put("code",200);
            map.put("msg","修改用户权限成功");
            map.put("object",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改用户权限失败，该用户不存在");
            map.put("object",null);
        }
        return map;
    }

    @GetMapping("/authority/selectAuthorityByUserId/{userId}")
    public Map<String,Object> selectAuthorityByUserId(@PathVariable Integer userId){
        Authority authority=authorityService.selectAuthorityByUserId(userId);
        Map<String,Object> map=new HashMap<>();
        if(authority!=null){
            map.put("code",200);
            map.put("msg","查询用户权限成功");
            map.put("object",authority);
        }
        else {
            map.put("code",500);
            map.put("msg","查询用户权限失败，该用户不存在");
            map.put("object",null);
        }
        return map;
    }

    @GetMapping("/authority/deleteAuthority/{userId}")
    public Map<String,Object> deleteAuthority(@PathVariable Integer userId){
        Map<String,Object> map=new HashMap<>();
        if(authorityService.deleteAuthority(userId)){
            map.put("code",200);
            map.put("msg","删除用户权限成功");
            map.put("object",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除用户权限失败，该用户不存在");
            map.put("object",null);
        }
        return map;
    }
}
